using System;
using System.Collections;
using BitMango.Diagnostics;
using UnityEngine;

public class OrientationObserver : MonoBehaviour
{
	public static ScreenOrientation Orientation
	{
		get
		{
			return OrientationObserver.orientation;
		}
	}

	private void setOrientation(ScreenOrientation ori)
	{
		OrientationObserver.orientation = ori;
		base.StartCoroutine(this.checkOrientationComplete(ori));
	}

	private static void UpdateCamResolution(ScreenOrientation ori)
	{
		Camera main = Camera.main;
		Debugger.Assert(main != null);
		tk2dCamera tk2dCamera = main.GetComponent<tk2dCamera>();
		if (tk2dCamera == null)
		{
			return;
		}
		if (tk2dCamera.InheritConfig != null)
		{
			tk2dCamera = tk2dCamera.InheritConfig;
		}
		if (ori == ScreenOrientation.LandscapeLeft)
		{
			tk2dCamera.nativeResolutionWidth = 960;
			tk2dCamera.nativeResolutionHeight = 640;
			tk2dCamera.UpdateCameraMatrix();
		}
		else if (ori == ScreenOrientation.Portrait)
		{
			tk2dCamera.nativeResolutionWidth = 640;
			tk2dCamera.nativeResolutionHeight = 960;
			tk2dCamera.UpdateCameraMatrix();
		}
	}

	private void Awake()
	{
		OrientationObserver.orientation = Screen.orientation;
		OrientationObserver.UpdateCamResolution(OrientationObserver.orientation);
		this.UpdateAndroidScreenLock();
	}

	private void Update()
	{
		if (OrientationObserver.IsPortrate())
		{
			if (OrientationObserver.orientation != ScreenOrientation.Portrait)
			{
				this.setOrientation(ScreenOrientation.Portrait);
			}
		}
		else if (OrientationObserver.orientation != ScreenOrientation.LandscapeLeft)
		{
			this.setOrientation(ScreenOrientation.LandscapeLeft);
		}
	}

	private void UpdateAndroidScreenLock()
	{
		bool flag = OrientationObserver.androidLock;
		if (flag != OrientationObserver.androidLock)
		{
			OrientationObserver.androidLock = flag;
			if (flag)
			{
				OrientationObserver.OrientationLock(flag);
				if (OrientationObserver.orientation != ScreenOrientation.Portrait)
				{
					Screen.orientation = ScreenOrientation.Portrait;
					this.setOrientation(ScreenOrientation.Portrait);
				}
			}
			else
			{
				OrientationObserver.OrientationLock(flag);
			}
		}
	}

	private void OnApplicationFocus()
	{
		this.UpdateAndroidScreenLock();
	}

	public static bool IsPortrate()
	{
		return OrientationObserver.androidLock || (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown) || (Screen.orientation != ScreenOrientation.LandscapeLeft && Screen.orientation != ScreenOrientation.LandscapeLeft && Screen.orientation != ScreenOrientation.LandscapeRight && (Screen.orientation != ScreenOrientation.AutoRotation || (!Screen.autorotateToLandscapeLeft && !Screen.autorotateToLandscapeRight)));
	}

	public static void OrientationLock(bool isLock)
	{
		if (OrientationObserver.androidLock && !isLock)
		{
			return;
		}
		Screen.autorotateToPortrait = !isLock;
		Screen.autorotateToPortraitUpsideDown = !isLock;
		Screen.autorotateToLandscapeRight = !isLock;
		Screen.autorotateToLandscapeLeft = !isLock;
		if (!isLock)
		{
			Screen.orientation = ScreenOrientation.AutoRotation;
		}
	}

	private IEnumerator checkOrientationComplete(ScreenOrientation orientation)
	{
		bool isOrientationComplete = false;
		OrientationObserver.UpdateCamResolution(orientation);
		while (!isOrientationComplete)
		{
			if (orientation == ScreenOrientation.Portrait)
			{
				isOrientationComplete = (Screen.height > Screen.width);
			}
			else if (orientation == ScreenOrientation.LandscapeLeft)
			{
				isOrientationComplete = (Screen.height < Screen.width);
			}
			yield return null;
		}
		if (OrientationObserver.onChange != null)
		{
			OrientationObserver.onChange(orientation);
		}
		Scene.SendMessage<IHandlerBase>("OnOrientationChanged", orientation, SendMessageOptions.DontRequireReceiver);
		yield break;
	}

	public static Action<ScreenOrientation> onChange;

	protected static ScreenOrientation orientation;

	private static bool androidLock;
}
